(function(jQuery){
    $.bundDate = function(){
        var date = {};
        /* 操作日期
         ---------------------------------------------------------------------------------------*/
        //获取年
        this.getYear = function(date){
            if (date == undefined) 
                date = new Date;
            return date.getFullYear();
        };
        //获取月
        this.getMonth = function(date){
            if (date == undefined) 
                date = new Date;
            var month = date.getMonth() + 1;
            if (month < 10) 
                month = "0" + month;
            return month;
        };
        //获取日
        this.getDay = function(date){
            if (date == undefined) 
                date = new Date;
            var day = date.getDate();
            if (day < 10) 
                day = "0" + day;
            return day;
        };
        //获取当前日期
        this.getDateToday = function(date){
            return this.getYear() + "-" + this.getMonth() + "-" + this.getDay();
        };
        //获取星期
        this.getWeek = function(date){
            if (date == undefined) 
                date = new Date;
            var week = date.getDay();
            var weekArr = ["日", "一", "二", "三", "四", "五", "六"];
            return weekArr[week];
        };
        //获取小时
        this.getHours = function(date){
            if (date == undefined) 
                date = new Date;
            return date.getHours();
        }, //获取分钟
        this.getMinutes = function(date){
            if (date == undefined) 
                date = new Date;
            return date.getMinutes();
        };
        //打开日期控件
        this.openDatePicek = function(year, month, day, callback){
            uexControl.openDatePicker(year, month, day);
            uexControl.cbOpenDatePicker = function(opCode, dataType, data){
                if (dataType == 1) {
                    data = JSON.parse(data);
                    if (data.month < 10) 
                        data.month = "0" + data.month;
                    if (data.day < 10) 
                        data.day = "0" + data.day;
                    var json = {
                        year: data.year,
                        month: data.month,
                        day: data.day,
                    };
                    callback(json);
                }
            }
        };
        //打开时间控件
        date.openTime = function(hours, minutes, callback){
            uexControl.openTimePicker(hours, minutes);
            uexControl.cbOpenTimePicker = function(opCode, dataType, data){
                data = JSON.parse(data);
                if (data.hour < 10) 
                    data.hour = "0" + data.hour;
                if (data.minute < 10) 
                    data.minute = "0" + data.minute;
                var json = {
                    hour: data.hour,
                    minute: data.minute
                };
                callback(json);
            }
        };
        
        
        //检查每月最后一天是几号
        this.checkLastDay = function(year, month){
            var maxDays = 31;
            if (month == 2) {
                if (parseInt(year) % 4 == 0) 
                    maxDays = 29;
                else 
                    maxDays = 28;
            }
            if (month == 4 || month == 6 || month == 9 || month == 11) 
                maxDays = 30;
            return month + "-" + maxDays;
        };
        
        
        //        this.iP = {};
        //页面初始化
        this.init = function(){
            //初始化->开始日期
            this.initialDate();
            //绑定->日期控件
            $("#startDate").on("click", this.openDate);
//			$("#startDate1").on("click", this.openDate);
        };
        this.initialDate = function(){
            //初始化->开始日期
            var newdate = new Date();
            var newtimems = newdate.getTime();
            newdate.setTime(newtimems);
            this.setDate($("#startDate"), this.getYear(newdate), this.getMonth(newdate), this.getDay(newdate));
//            this.setDate($("#startDate1"), this.getYear(newdate), this.getMonth(newdate), this.getDay(newdate));
	    };
        //打开日期控件
        this.openDate = function(){
            var that = $(this);
            var date = JSON.parse(that.attr("date"));
            $.bundDate().openDatePicek(date.year, date.month, date.day, function(data){
                 $.bundDate().setDate(that, data.year, data.month, data.day);
            })
        };
        //设置日期、星期
        this.setDate = function(elem, year, month, day){
            var json = {
                "year": year,
                "month": month,
                "day": day,
            }
//			if(elem.selector=="#startDate1"){
//				 elem.text(year + "-" + month);
//			}else{
				 elem.text(year + "-" + month + "-" + day);
//			}
            elem.attr("date", JSON.stringify(json));
            elem.attr("need", year + "-" + month + "-" + day);
            var today = year + "-" + month + "-" + day;
            var zuo = getDay(-1);
            var jin = getDay(0);
            var ming = getDay(1);
            if (today == zuo) {
                $("#day").text("昨天");
            }
            else 
                if (today == jin) {
                    $("#day").text("今天");
                }
                else 
                    if (today == ming) {
                        $("#day").text("明天");
                    }
                    else {
                        $("#day").text("");
                    }
            
            var weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
            var dateStr = (year + "-" + month + "-" + day);
            var myDate = new Date(Date.parse(dateStr.replace(/-/g, "/")));
            $("#week").text(weekDay[myDate.getDay()]);
            
        };
        function getDay(day){
            var today = new Date();
            var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
            today.setTime(targetday_milliseconds); //注意，这行是关键代码  
            var tYear = today.getFullYear();
            var tMonth = today.getMonth();
            var tDate = today.getDate();
            tMonth = doHandleMonth(tMonth + 1);
            tDate = doHandleMonth(tDate);
            return tYear + "-" + tMonth + "-" + tDate;
        }
        
        function doHandleMonth(month){
            var m = month;
            if (month.toString().length == 1) {
                m = "0" + month;
            }
            return m;
        }
        
        this.selectInit = function(){
            var date = JSON.parse(getStorage("data")).time;
            var dateArr = date.split("-");
            this.setDateTop(dateArr[0], dateArr[1], dateArr[2]);
            $("#date").removeClass("uhide");
            $("#date>div").on("click", this.selectDate);
        };
        //选择日期
        this.selectDate = function(){
            var dateString = $(this).attr("date");
            var date = JSON.parse(dateString);
            $.bundDate().setDateTop(date.year, date.month, date.day);
            var data = JSON.parse(getStorage("data"));
            data.time = date.year + "-" + date.month + "-" + date.day;
            setStorage("data",JSON.stringify(data));
            $.flySearch().load();
        }
		//获取本地时间字符串格式
        this.CurentTime=function(){
            var now = new Date();
            var year = now.getFullYear(); //年
            var month = now.getMonth() + 1; //月
            var day = now.getDate(); //日
            var hh = now.getHours(); //时
            var mm = now.getMinutes(); //分
            var second = now.getSeconds();
            var clock = year + "-";
            if (month < 10) 
                clock += "0";
            clock += month + "-";
            if (day < 10) 
                clock += "0";
            clock += day + " ";
            if (hh < 10) 
                clock += "0";
            clock += hh + ":";
            if (mm < 10) 
                clock += '0';
            clock += mm + ":";
            if (second < 10) 
                second += '0';
            clock += second;
            return (clock);
        }
		
        //设置日期
        this.setDateTop = function(year, month, day){
            var today = new Date($.bundDate().getYear(), $.bundDate().getMonth() - 1, $.bundDate().getDay());
            var yesterday = new Date($.bundDate().getYear(), $.bundDate().getMonth() - 1, $.bundDate().getDay());
            yesterday.setDate(yesterday.getDate() - 1);
            var tomorrow = new Date($.bundDate().getYear(), $.bundDate().getMonth() - 1, $.bundDate().getDay());
            tomorrow.setDate(tomorrow.getDate() + 1);
            var curDate = new Date(year, month - 1, day);
            var prevDate = new Date(year, month - 1, day);
            prevDate.setDate(curDate.getDate() - 1);
            var nextDate = new Date(year, month - 1, day);
            nextDate.setDate(curDate.getDate() + 1);
            var prev = {
                "year": $.bundDate().getYear(prevDate),
                "month": $.bundDate().getMonth(prevDate),
                "day": $.bundDate().getDay(prevDate)
            }
            var cur = {
                "year": year,
                "month": month,
                "day": day
            };
            var next = {
                "year": $.bundDate().getYear(nextDate),
                "month": $.bundDate().getMonth(nextDate),
                "day": $.bundDate().getDay(nextDate)
            }
            var prevShow = prev.month + "-" + prev.day;
            var curShow = cur.month + "-" + cur.day;
            var nextShow = next.month + "-" + next.day;
            switch (prevDate.getTime()) {
                case yesterday.getTime():
                    prevShow = "昨天";
                    break;
                case today.getTime():
                    prevShow = "今天";
                    break;
                case tomorrow.getTime():
                    prevShow = "明天";
                    break;
            }
            switch (curDate.getTime()) {
                case yesterday.getTime():
                    curShow = curShow + "(昨天)";
                    break;
                case today.getTime():
                    curShow = curShow + "(今天)";
                    break;
                case tomorrow.getTime():
                    curShow = curShow + "(明天)";
                    break;
            }
            switch (nextDate.getTime()) {
                case yesterday.getTime():
                    nextShow = "昨天";
                    break;
                case today.getTime():
                    nextShow = "今天";
                    break;
                case tomorrow.getTime():
                    nextShow = "明天";
                    break;
            }
            $("[prevDate]").attr("date", JSON.stringify(prev)).children("[dateShow]").text(prevShow);
            $("[curDate]").attr("date", JSON.stringify(cur)).children("[dateShow]").text(curShow);
            $("[nextDate]").attr("date", JSON.stringify(next)).children("[dateShow]").text(nextShow);
        }
        return this;
    }
})(jQuery);
